#include <zmq.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdint.h>
#include "ipc.h"


GMainLoop *loop;
GTimer *tm;
gulong ms;
gdouble tt;

void sig3(GVariant *par)
{
	static uint32_t c = 0;
	G_LOCK_DEFINE_STATIC(sig3);

	G_LOCK(sig3);

	g_variant_get(par, "(u)", &c);
	g_variant_unref(par);
	if (c < 10000) {
		c++;
		ipc_raise("Sig2", g_variant_new("(u)", c));

	}
	else {
		g_timer_stop(tm);
		tt = g_timer_elapsed(tm, &ms);
		printf("time: %f usec\n", (tt/c)*1000000);
		g_main_loop_quit(loop);
		ipc_release();
	}
	G_UNLOCK(sig3);
}

int main (void)
{
	GVariant *var, *ret;
	ipc_init("test_client");
	char *buf;
	tm = g_timer_new();
	int request_nbr;
	ipc_signal("test", "Sig3", sig3);
	loop = g_main_loop_new(NULL, FALSE);

	printf("Start non-block signal test\n");
	//ipc_raise("Sig1", g_variant_new("()"));
	//ipc_raise("Sig1", g_variant_new("()"));/*Test non-block signal*/



	printf("\nStart ipc_call latency test\n");
	g_timer_start(tm);
	for (request_nbr = 0; request_nbr != 10000; request_nbr++) {
		var = g_variant_new("(ssu)", "Hello", "World", request_nbr);
		ret = ipc_call("test", "Test1", var);
		buf = g_variant_print(ret, FALSE);
	}
	g_timer_stop(tm);
	tt = g_timer_elapsed(tm, &ms);
	printf("time: %f usec\n", tt*100);

	g_timer_start(tm);
	printf("\nStart signal round-trip test\n");
	sig3(g_variant_new("(u)", 0));

	g_main_loop_run(loop);
	return 0;
}
